#include "maplayer.h"

MapLayer::MapLayer()
    : m_type(OtherLayer), m_visible(true)
{}

MapLayer::MapLayer(const QString &name, LayerType type, const QString &source, bool visible)
    : m_name(name), m_type(type), m_source(source), m_visible(visible)
{}

QString MapLayer::name() const
{
    return m_name;
}

void MapLayer::setName(const QString &name)
{
    m_name = name;
}

MapLayer::LayerType MapLayer::type() const
{
    return m_type;
}

void MapLayer::setType(LayerType type)
{
    m_type = type;
}

QString MapLayer::source() const
{
    return m_source;
}

void MapLayer::setSource(const QString &source)
{
    m_source = source;
}

bool MapLayer::isVisible() const
{
    return m_visible;
}

void MapLayer::setVisible(bool visible)
{
    m_visible = visible;
}

QJsonObject MapLayer::toJson() const
{
    QJsonObject json;
    json["name"] = m_name;
    json["type"] = typeToString();
    json["source"] = m_source;
    json["visible"] = m_visible;
    return json;
}

MapLayer MapLayer::fromJson(const QJsonObject &json)
{
    MapLayer layer;
    layer.setName(json["name"].toString());
    layer.setType(stringToType(json["type"].toString()));
    layer.setSource(json["source"].toString());
    layer.setVisible(json["visible"].toBool(true));
    return layer;
}

QString MapLayer::typeToString() const
{
    switch (m_type) {
    case GridLayer: return "grid";
    case MBTilesLayer: return "mbtiles";
    case AmapLayer: return "amap";
    case TianDiTuLayer: return "tianditu";
    default: return "other";
    }
}

MapLayer::LayerType MapLayer::stringToType(const QString &typeStr)
{
    if (typeStr == "grid") return GridLayer;
    if (typeStr == "mbtiles") return MBTilesLayer;
    if (typeStr == "amap") return AmapLayer;
    if (typeStr == "tianditu") return TianDiTuLayer;
    return OtherLayer;
}